package com.apes.hr.group.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.hr.group.repository.DingDingPersonRepository;
import com.apes.scm.masterdata.party.model.Person;
import com.apes.scm.masterdata.party.repository.PersonRepository;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：钉钉群进群人员(做开店申请创建群设置进群领导用)
 * @author ZhengZX
 * @create 2020年10月10日11:05:36
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DingDingGroupPerson.CONST_TABLE_NAME)
public class DingDingGroupPerson extends Model{
    public static final String CONST_TABLE_NAME = "hr_ding_ding_group_person";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "钉钉建群进群人员")
    @Comment("编码")
    private String id; //编码

    @Column(name = "group_userid_list", length = 4000)
    @Comment("进群人员编码")
    private String groupUserIdList; //进群人员列表

    @Transient
    private Set<DingDingPerson> groupUserIdSet;

    public Set<DingDingPerson> getGroupUserIdSet() {
        if (isWrite() || isLoad()) {
            this.groupUserIdSet = convertPerson(this.groupUserIdList);
        }
        return groupUserIdSet;
    }
    public void setGroupUserIdSet(Set<DingDingPerson> managerUseridSet) {
        this.groupUserIdSet = managerUseridSet;
        if (!isWrite()) {
            this.groupUserIdList = toConvertPerson(this.groupUserIdSet);
        }
    }

    //拆分字符串
    private Set<DingDingPerson> convertPerson(String source) {
        if (StringUtil.isEmpty(source)) return null;
        Set<DingDingPerson> persons = new HashSet<>();
        String[] rows = source.split("\\|");    //056147580920954696|04211截取成两个钉钉
        for (String person : rows) {
            DingDingPerson tempPerson = SpringManager.getBean(DingDingPersonRepository.class).findAll(
                    JpaDsl.toCriteriaByEq(
                            "dingDingUserId", person
                    )
            ).stream().findFirst().orElse(null);
            persons.add(tempPerson);
        }
        return persons;
    }

    //拼接字符串
    private String toConvertPerson(Set<DingDingPerson> persons) {
        StringBuffer sb = new StringBuffer();
        for (DingDingPerson person : persons) {
            sb.append(person.getDingDingUserId());
            sb.append("|");
        }
        if (sb.length()>0){
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }
}
